<?php

namespace App\Http\Controllers\Rbh;

use App\Exceptions\BusinessException;
use App\Http\Controllers\Controller;
use App\Http\Resources\AdminUserModelResource;
use App\Models\AdminUserModel;
use App\Utils\ApiResponse;
use App\Utils\ControllerModelTrait;
use App\Utils\ResponseEnum;
use App\Utils\VerifyRequestInput;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Hash;

class AdminUserController extends Controller
{
    use ApiResponse, VerifyRequestInput, ControllerModelTrait;

    /**
     * 登录
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function login(Request $request): JsonResponse
    {
        $username = $this->verifyString('username');
        $password = $this->verifyString('password');
        $user = AdminUserModel::where('username', $username)->first();
        if (!$user || !Hash::check($password, $user->password))
            return $this->fail(ResponseEnum::SERVICE_LOGIN_ACCOUNT_ERROR);
        $user->access_token = $user->createToken('user-token')->plainTextToken;
        $user->save();
        return $this->success(new AdminUserModelResource($user));
    }

    /**
     * 获取用户信息
     * @param Request $request
     * @return JsonResponse
     */
    public function info(Request $request): JsonResponse
    {
        if ($request->input('access_token'))
            $user = AdminUserModel::where('access_token', $request->input('access_token'))->first();
        else
            $user = AdminUserModel::where('openid', $request->input('openid'))->first();
        if (!$user)
            return $this->fail(ResponseEnum::CLIENT_DELETED_ERROR);
        return $this->success(new AdminUserModelResource($user));
    }

    /**
     * 退出
     * @return JsonResponse
     */
    public function logout(): JsonResponse
    {
        return $this->success();
    }

    /**
     * 列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getList(Request $request): JsonResponse
    {
        $Model = AdminUserModel::where('department', 1);
        if  ($request->has('username'))
            $Model = $Model->where('username', $request->input('username'));
        if  ($request->has('nickname'))
            $Model = $Model->where('nickname', $request->input('nickname'));
        $pagination = $this->getPagedData($Model, $request);
        if ($pagination instanceof LengthAwarePaginator)
            return $this->successPaginate($pagination);
        else
            return $this->successPaginate(new Collection());
    }

    /**
     * 保存数据
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
    */
    public function doSave(Request $request): JsonResponse
    {
        $this->verifyString('username');
        $this->verifyString('password');
        $this->verifyString('role');
        $data = $request->except(['created_at', 'updated_at']);
        if ($request->has('password'))
            $data['password'] = bcrypt($request->input('password'));
        $this->saveData(new AdminUserModel(), $data);
        return $this->success($data);
    }

    /**
     * 删除
     * @return JsonResponse
     * @throws BusinessException
     */
    public function doDelete(): JsonResponse
    {
        $ids = $this->verifyString('ids', null);
        $this->deleteData(new AdminUserModel(), $ids);
        return $this->success([]);
    }
}
